/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.deposit;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import src.domain.deposit.Bilyet;
import src.domain.deposit.Deproduct;
import src.domain.security.Users;
import src.services.deposit.BilyetsimModel;
import src.services.deposit.DeproductModel;
import src.services.gl.TaxModel;
import src.util.Constanta;
import src.util.Utils;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */

@ManagedBean
@SessionScoped
public class BilyetsimBean implements Serializable{
    private boolean editMode = false,  insertionMode = true, authorized=false;
    private Bilyet bilyet;
    private List<Bilyet> bilyets;
    private DeproductModel prodHelper;

    private List<SelectItem> avblProd;
    private BigDecimal taxRate = BigDecimal.ZERO;
    private BigDecimal percentDivisor = new BigDecimal(100);
    private Users user;
    private String taxAmounts, interestAmounts;    
    
    public BilyetsimBean() {
        this.prodHelper = new DeproductModel(); 
        this.bilyets = new ArrayList<>();
        newData();
        editMode = false;  
        authorized=false;
    }
       
    public Users getUser() {
        return user;
    }

    public void setUser(Users user) {
        this.user = user;
    }
        
    public Bilyet getBilyet() {
        return bilyet;
    }

    public void setBilyet(Bilyet bilyet) {
        this.bilyet = bilyet;
    }
    
    public boolean isEditMode() {
        return editMode;
    }

    public boolean isAuthorized() {
        return authorized;
    }
    
    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    public String getInterestAmounts() {
        return interestAmounts;
    }

    public String getTaxAmounts() {
        return taxAmounts;
    }
    
    public List<SelectItem> getAvblProd() {
        return avblProd;
    }

    public List<Bilyet> getBilyets() {
        return bilyets;
    }
            
    public void initForm(){
        this.avblProd = this.prodHelper.listAll();
        this.editMode = true;
        this.authorized = true;
        taxRate = BigDecimal.ZERO;
    }
    
    public final void newData(){
        initForm();
        if (bilyet!=null) bilyet = null;
        bilyet = new Bilyet();
        insertionMode = true;
    }
    
    public void calcInterestAmount() {  
        BigDecimal amount = BigDecimal.ZERO;
        if (bilyet.getInterestRate() !=null && bilyet.getPrincipleAmount() != null) {
            BigDecimal intRate = bilyet.getInterestRate().divide(percentDivisor,2, RoundingMode.HALF_UP);
            amount = bilyet.getPrincipleAmount().multiply(intRate);
        }   
        bilyet.setInterestAmount(amount);
    }  
        
    public void calcTaxAmount() {  
        BigDecimal taxAmount = BigDecimal.ZERO;
        if (bilyet.getPrincipleAmount() !=null && bilyet.getInterestAmount() != null) {
            BigDecimal period = BigDecimal.valueOf(bilyet.getDeproduct().getNumOfPeriod())
                    .divide(BigDecimal.valueOf(30), 0, RoundingMode.UP);
            
            BigDecimal intmonth = bilyet.getInterestAmount().divide(period, 0, RoundingMode.CEILING);
            
            if (intmonth.compareTo(Constanta.MAX_INTEREST_TAXED) > 0) 
               taxAmount = bilyet.getInterestAmount().multiply(taxRate);
        }    
        bilyet.setTaxAmount(taxAmount);
    }  
    
    public void calculateAmount() {  
        calcInterestAmount();
        calcTaxAmount();
    }  
        
    public void handleProdChange() {  
        DeproductModel helper = new DeproductModel();
        Deproduct produk= new Deproduct();
        Integer id = bilyet.getDeproduct().getDeproductId();
        if (id != null) {
            produk = helper.getById(id);
            taxRate = new TaxModel().getById(produk.getTaxId()).getTaxRate().divide(percentDivisor, 2, RoundingMode.HALF_DOWN);
           calculateAmount();
            
        }
        bilyet.setDeproduct(produk);
    }  
    
    
    public void simulasi() {
        if (this.bilyet.getDeproduct().getPeriodOpt().equals(Constanta.DEP_MONTH)) 
            this.bilyets = new BilyetsimModel().simulation(bilyet);
        else this.bilyets = new BilyetsimModel().simulationHari(bilyet);
        
        BigDecimal taxAmount = BigDecimal.ZERO;
        BigDecimal intAmount = BigDecimal.ZERO;
        
        for (int i=0; i<bilyets.size(); i++) {
            Bilyet depo =  bilyets.get(i);
            intAmount = intAmount.add(depo.getInterestAmount());
            taxAmount = taxAmount.add(depo.getTaxAmount());
        }
        
        taxAmounts = Utils.formatNumber(taxAmount);
        interestAmounts = Utils.formatNumber(intAmount);
        //interestAmounts = Utils.formatNumber(totalInterestAmount);
        //amounts = Utils.formatNumber(totalAmount);
        
    }
    
    public void simulasi2() {
        this.bilyets = new BilyetsimModel().simulationHari(bilyet);
        
        BigDecimal taxAmount = BigDecimal.ZERO;
        BigDecimal intAmount = BigDecimal.ZERO;
        
        
        for (int i=0; i<bilyets.size(); i++) {
            Bilyet depo =  bilyets.get(i);
            intAmount = intAmount.add(depo.getInterestAmount());
            taxAmount = taxAmount.add(depo.getTaxAmount());
        }
        
        taxAmounts = Utils.formatNumber(taxAmount);
        interestAmounts = Utils.formatNumber(intAmount);
        //interestAmounts = Utils.formatNumber(totalInterestAmount);
        //amounts = Utils.formatNumber(totalAmount);
        
    }
    
    
    
   
}
